<?php

namespace app\admin\controller;


class Inform extends Base
{
    //名言列表
    public function all()
    {
        //查询名言
        $informs = model('Inform')->order('create_time','desc')->paginate(10);
        //模板变量
        $viewData = [
            'informs' => $informs
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //名言添加
    public function add()
    {
        //接收数据
        if (request()->isAjax()){
            $data = [
                'id' => input('post.id'),
                'title' => input('post.title'),
                'content' => input('post.content')
            ];
            //将数据传递给模型并把结果返回给$result
            $result = model('Inform')->add($data);
            if ($result == 1){
                $this->success('添加成功','admin/inform/all');
            }else{
                $this->error($result);
            }
        }
        return view();
    }

    //名言编辑
    public function edit()
    {
        //接收数据
        if (request()->isAjax()){
            $data = [
                'id' => input('post.id'),
                'title' => input('post.title'),
                'content' => input('post.content')
            ];
            //将数据传递给模型并把结果返回给$result
            $result = model('Inform')->edit($data);
            if ($result == 1){
                $this->success('更新成功','admin/inform/all');
            }else{
                $this->error($result);
            }
        }
        //查询出标题id
        $informInfo = model('Inform')->find(input('id'));
        //模板变量
        $viewData = [
            'informInfo' => $informInfo
        ];
        //渲染
        $this->assign($viewData);
        return view();
    }

    //名言删除
    public function del()
    {
        $informInfo = model('Inform')->find(input('post.id'));
        $result = $informInfo->delete();
        if ($result){
            $this->success('删除成功','admin/inform/all');
        }else{
            $this->error('删除失败');
        }
    }
}
